//
//  MZMoewViewController.m
//  OurTourism
//
//  Created by 孟镇 on 15/11/26.
//  Copyright © 2015年 Devil_Coming. All rights reserved.
//

#import "MZMoreViewController.h"
#import "Header.h"
#import "MZRecommendCollectionViewCell.h"
#import "MZRecommendModel.h"
#import "MZRecommendPushViewController.h"

@interface MZMoreViewController () <UICollectionViewDataSource, UICollectionViewDelegate>

@property(nonatomic, strong) UICollectionView *collectionView;
@property(nonatomic, strong) NSMutableArray *dataArr;
@property(nonatomic, strong) MBProgressHUD *HUB;
@property(nonatomic, strong) NSDictionary *dataDic;

@end

@implementation MZMoreViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    self.navigationItem.title = self.tempModel.title;
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 25, 25);
    [backBtn setImage:[UIImage imageNamed:@"iconfont-iconfonticonfontjiantouzuo11@2x"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
    self.dataArr = [NSMutableArray array];
    
    [self createCollectionView];
    [self prepareData];
    
    self.HUB = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self.HUB show:YES];
    self.HUB.labelText = @"正在加载";
}


#pragma mark - 创建collectionView
- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    layout.minimumLineSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 0, 10);
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGTH - 20) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:250 / 255. green:246 / 255. blue:232 / 255. alpha:1];
    [self.collectionView registerClass:[MZRecommendCollectionViewCell class] forCellWithReuseIdentifier:@"moreRecommendItem"];
    [self.view addSubview:self.collectionView];
}

#pragma mark - 准备数据
- (void)prepareData
{
    NSString *str = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%ld/", self.tempModel.index.integerValue];
    [MZNetHandle getUrl:str cookie:nil result:^(id result) {
       
        NSArray *arr = result[@"data"];
        self.dataArr = [MZRecommendModel transformWithArray:arr];
        self.dataDic = result;
        
        [self.collectionView reloadData];
        [self.HUB removeFromSuperview];
    }];
    
}

#pragma mark - collectionView协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MZRecommendCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"moreRecommendItem" forIndexPath:indexPath];
    cell.temp = self.dataArr[indexPath.item];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MZRecommendPushViewController *rpVC = [[MZRecommendPushViewController alloc] init];
//    rpVC.tempModel = self.dataDic[@"data"][indexPath.item];
    rpVC.tempModel = [MZRecommendModel transformWithArray:self.dataDic[@"data"]][indexPath.item];
    [self.navigationController pushViewController:rpVC animated:YES];
    
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
